package com.fenda.qq;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

public class QQMainFrame {

	public static void main(String[] args) {
		start(null, null);
	}

	public static void start(String user, String[] friends) {
		JFrame frame = new JFrame("主界面");
		frame.setBounds(1500, 0, 300, 900);

		JPanel mainPanel = new JPanel();
		//mainPanel.setBorder(new LineBorder(Color.BLACK,5));
		frame.setContentPane(mainPanel);

		JPanel myInfoPanel = new JPanel();
		myInfoPanel.setBounds(5, 10, 275, 100);
		ImageIcon ii = new ImageIcon("d:\\QQImg\\1.jpg");
		JLabel label = new JLabel(ii);
		label.setBounds(3, 3, ii.getIconWidth(), ii.getIconHeight());
		myInfoPanel.add(label);
		JLabel label2 = new JLabel("往事随风");
		Font font = new Font("宋体", Font.PLAIN, 25);
		label2.setFont(font);
		label2.setBounds(105, 30, 100, 30);
		myInfoPanel.add(label2);
		//myInfoPanel.setBorder(new LineBorder(Color.RED));
		myInfoPanel.setLayout(null);
		mainPanel.add(myInfoPanel);

		JPanel addFriendsPanel = new JPanel();
		addFriendsPanel.setBounds(5, 110, 275, 50);
		JTextField searchTxt = new JTextField(12);
		searchTxt.setFont(font);
		addFriendsPanel.add(searchTxt);
		Font font1 = new Font("黑体", Font.PLAIN, 20);
		JButton addBtn = new JButton("添加");
		addBtn.setFont(font1);
		int index = 0;
		addBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				String friend = searchTxt.getText();
				if(user.equals(friend)){
					JOptionPane.showMessageDialog(null, "不能添加自己！","", JOptionPane.ERROR_MESSAGE);
				}
				//1.获取添加好友状态返回
				QQResponse qqResponse = QQClient.addFriend(user, friend);
				if(qqResponse.getSuccess()){//如果添加好友成功
					System.out.println("添加好友" + friend + "成功");
					//2.如果添加成功，刷新好友列表
					refreshFriendPanel(user,mainPanel);
				}
			}
		});
		addFriendsPanel.add(addBtn);
		//addFriendsPanel.setBorder(new LineBorder(Color.RED));
		mainPanel.add(addFriendsPanel);

		Random r = new Random();
		if(friends != null && friends.length > 0){
			for(String friend : friends){
				JPanel friendsPanel = new JPanel();
				friendsPanel.setBounds(5, 160+60*index, 275, 60);
				ImageIcon ii1 = new ImageIcon("d:\\QQImg\\" +  (r.nextInt(5) + 2) +".jpg");
				JLabel label1 = new JLabel(ii1);
				label1.setBounds(3, 3, ii.getIconWidth(), ii.getIconHeight());
				friendsPanel.add(label1);
				JLabel label22 = new JLabel(friend);
				label22.setFont(font);
				label22.setBounds(105, 30, 100, 30);
				friendsPanel.add(label22);
				mainPanel.add(friendsPanel);
				friendsPanel.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {
						if(e.getClickCount() == 2){//双击
							QQChatFrame.start(user,friend);
						}
					}
				});
				index ++;
			}
		}

		frame.setLayout(null);//不使用系统布局方式
		//frame.setLayout(new FlowLayout());//流式布局
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗体退出程序
		frame.setVisible(true);
	}

	/**
	 * 读取好友信息
	 * @param user
	 * @param mainPanel
	 */
	private static void refreshFriendPanel(String user, JPanel mainPanel) {
		QQRequest request = new QQRequest();
		request.setUsername(user);
		//刷新好友列表之前先从服务器加载好友列表
		QQResponse response = QQClient.findFriendsInfo(user);
		if(response.getFriends() != null){//如果好友列表不为空
			int index = 0;
			Random r = new Random();
			for(String friend : response.getFriends()){
				JPanel friendsPanel = new JPanel();
				friendsPanel.setBounds(5, 160+60*index, 275, 60);
				ImageIcon ii1 = new ImageIcon("F:\\QQImgs\\" +  (r.nextInt(5) + 2) +".jpg");
				JLabel label1 = new JLabel(ii1);
				label1.setBounds(3, 3, ii1.getIconWidth(), ii1.getIconHeight());
				friendsPanel.add(label1);
				JLabel label22 = new JLabel(friend);
				label22.setBounds(105, 30, 100, 30);
				friendsPanel.add(label22);
				mainPanel.add(friendsPanel);
				index ++;
			}
			mainPanel.repaint();//刷新页面布局
		}
	}

}
